# -*- coding: utf-8 -*-

"""
PyoCDVD (http://code.google.com/p/pyocdvd/)
Media Organizer (CD, DVD, etc)

Developers:
    Bruno Pitteli Gonçalves <scorninpc(*)gmail(*)com>
    Paul Eipper <lkraider(*)gmail(*)com>

Licence: GNU GENERAL PUBLIC LICENSE v3

"""

import os
import sys


class PyoCDVD_Plugin(object):
    '''Base plugin class which should be inherited by other plugins.
    This class implements a singleton and defines an abstract method.'''

    filetypes = ['*']
    _instance = None

    def __new__(cls, *args, **kwargs):
        if not cls._instance:
            cls._instance = object.__new__(cls, *args, **kwargs)
        return cls._instance

    def run(self, filepath):
        filepath = filepath.encode(sys.getfilesystemencoding())
        file_extension = os.path.splitext(filepath)[-1:]
        if ('*' in self.filetypes) or (file_extension in self.filetypes):
            return self.process_file(filepath)

    def process_file(self, filepath):
        '''Abstract method - should be implemented by each plugin.'''
        import inspect
        caller = inspect.getouterframes(inspect.currentframe())[1][3]
        raise NotImplementedError(caller + ' must be implemented in subclass')